package labox.innovation.gameserver.skills.l2skills;

import labox.innovation.config.Config;
import labox.innovation.gameserver.datatables.Experience;
import labox.innovation.gameserver.datatables.NpcTemplateTable;
import labox.innovation.gameserver.idfactory.IdFactory;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.L2Skill;
import labox.innovation.gameserver.model.actor.L2Character;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.model.actor.instance.L2SummonInstance;
import labox.innovation.gameserver.templates.StatsSet;
import labox.innovation.gameserver.templates.chars.NpcTemplate;

/**
 * �ٻ�����
 * @author zjj
 *
 */
public class L2SkillSummon extends L2Skill
{
	public static final int SKILL_CUBIC_MASTERY = 143;

	private int     _npcId;
	private float   _expPenalty;

	// What is the total lifetime of summons (in millisecs)
	private final int _summonTotalLifeTime;
	// How much lifetime is lost per second of idleness (non-fighting)
	private final int _summonTimeLostIdle;
	// How much time is lost per second of activity (fighting)
	private final int _summonTimeLostActive;

	// item consume time in milliseconds
	private final int _itemConsumeTime;
	// item consume count over time
	private final int _itemConsumeOT;
	// item consume id over time
	private final int _itemConsumeIdOT;
	// how many times to consume an item
	private final int _itemConsumeSteps;

	public L2SkillSummon(StatsSet set)
	{
		super(set);

		_npcId      = set.getInteger("npcId", 0); // default for undescribed skills
		_expPenalty = set.getFloat ("expPenalty", 0.f);

		_summonTotalLifeTime= set.getInteger("summonTotalLifeTime", 1200000);  // 20 minutes default
		_summonTimeLostIdle= set.getInteger("summonTimeLostIdle", 0);
		_summonTimeLostActive= set.getInteger("summonTimeLostActive", 0);

		_itemConsumeOT = set.getInteger("itemConsumeCountOT", 0);
		_itemConsumeIdOT = set.getInteger("itemConsumeIdOT", 0);
		_itemConsumeTime = set.getInteger("itemConsumeTime", 0);
		_itemConsumeSteps = set.getInteger("itemConsumeSteps", 0);
	}

	public boolean checkCondition(L2Character activeChar)
	{
		if (activeChar instanceof FuzePcInstance)
		{
			FuzePcInstance player = (FuzePcInstance)activeChar;
			if (player.getPet() != null)
			{
				activeChar.sendMessage("You already have a pet.");
				return false;
			}

		}
		return super.checkCondition(activeChar, null, false);
	}

	@Override
	public void useSkill(L2Character caster, L2Object[] targets)
	{
		if ( !(caster instanceof FuzePcInstance) )
			return;

		FuzePcInstance activeChar = (FuzePcInstance) caster;

		if (_npcId == 0)
		{
			activeChar.sendMessage("Summon skill "+getId()+" not described yet");
			return;
		}

//		if (_isCubic)
//		{
//			if (targets.length > 1) //Mass cubic skill
//			{
//				for (L2Object obj: targets)
//				{
//					if (!(obj instanceof L2PcInstance)) continue;
//					L2PcInstance player = ((L2PcInstance)obj);
//					int mastery = player.getSkillLevel(SKILL_CUBIC_MASTERY);
//					if (mastery < 0)
//						mastery = 0;
//                    if (mastery == 0 && !player.getCubics().isEmpty())
//					{
//						//Player can have only 1 cubic - we shuld replace old cubic with new one
//                        for (L2CubicInstance c: player.getCubics().values())
//                        {
//                            c.stopAction();
//                            c = null;
//                        }
//						player.getCubics().clear();
//					}
//                    //TODO: Should remove first cubic summoned and replace with new cubic
//                    if (player.getCubics().containsKey(_npcId))
//                    {
//                        L2CubicInstance cubic = player.getCubic(_npcId);
//                        cubic.stopAction();
//                        cubic.cancelDisappear();
//                        player.delCubic(_npcId);
//                    }
//					if (player.getCubics().size() > mastery) continue;
//					//
//					if (player == activeChar)
//						player.addCubic(_npcId, getLevel(), getPower(), _activationtime, _activationchance, _summonTotalLifeTime, false);
//					else // given by other player
//						player.addCubic(_npcId, getLevel(), getPower(), _activationtime, _activationchance, _summonTotalLifeTime, true);
//					player.broadcastUserInfo();
//				}
//				return;
//			}
//			else //normal cubic skill
//			{
//				int mastery = activeChar.getSkillLevel(SKILL_CUBIC_MASTERY);
//				if (mastery < 0)
//					mastery = 0;
//                if (activeChar.getCubics().containsKey(_npcId))
//                {
//                    L2CubicInstance cubic = activeChar.getCubic(_npcId);
//                    cubic.stopAction();
//                    cubic.cancelDisappear();
//                    activeChar.delCubic(_npcId);
//                }
//				if (activeChar.getCubics().size() > mastery) {
//					if (Config.DEBUG)
//						_log.fine("player can't summon any more cubics. ignore summon skill");
//					activeChar.sendPacket(new SystemMessage(SystemMessageId.CUBIC_SUMMONING_FAILED));
//					return;
//				}
//                activeChar.addCubic(_npcId, getLevel(), getPower(), _activationtime, _activationchance, _summonTotalLifeTime, false);
//				activeChar.broadcastUserInfo();
//				return;
//			}
//		}// end isCubic

		if (activeChar.getPet() != null || activeChar.isMounted()) {
			if (_log.isDebugEnabled())
				_log.debug("player has a pet already. ignore summon skill");
			return;
		}

		L2SummonInstance summon;
		NpcTemplate summonTemplate = NpcTemplateTable.getInstance().getTemplate(_npcId);
		if (summonTemplate == null) 
		{
			_log.warn("Summon attempt for nonexisting NPC ID: {} , skill ID: {}",_npcId , this.getId());
			return; // npcID doesn't exist
		}
//		if (summonTemplate.type.equalsIgnoreCase("L2SiegeSummon"))
//			summon = new L2SiegeSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, this);
//		else 
//		if (summonTemplate.type.equalsIgnoreCase("L2MerchantSummon"))
//			summon = new L2MerchantSummonInstance(IdFactory.getInstance().getNextId(), summonTemplate, activeChar, this);
//		else
			summon = new L2SummonInstance(IdFactory.getInstance().getNextId(IdFactory.ID_SUMMON), summonTemplate, activeChar, this);

        summon.setName(summonTemplate.name);
        summon.setTitle(activeChar.getName());
        summon.setExpPenalty(_expPenalty);
        if (summon.getLevel() >= Experience.LEVEL.length)
        {
            summon.getStat().setExp(Experience.LEVEL[Experience.LEVEL.length - 1]);
            _log.warn("Summon ( {} ) NpcID: {} has a level above 75. Please rectify.", summon.getName(), summon.getNpcId());
        }
        else
        {
            summon.getStat().setExp(Experience.LEVEL[(summon.getLevel() % Experience.LEVEL.length)]);
        }
		summon.setCurrentHp(summon.getMaxHp());
		summon.setCurrentMp(summon.getMaxMp());
//		summon.setHeading(activeChar.getHeading());
//    	summon.setRunning();
//    	if (!(summon instanceof L2MerchantSummonInstance))
//    		activeChar.setPet(summon);

    	FuzeWorld.getInstance().enterWorld(summon);
        summon.spawnMe( activeChar.getX()+50, activeChar.getY()+100 );
	}


	/**
	 * @return Returns the itemConsume count over time.
	 */
	public final int getTotalLifeTime()
	{
		return _summonTotalLifeTime;
	}

	/**
	 * @return Returns the itemConsume count over time.
	 */
	public final int getTimeLostIdle()
	{
		return _summonTimeLostIdle;
	}

	/**
	 * @return Returns the itemConsumeId over time.
	 */
	public final int getTimeLostActive()
	{
		return _summonTimeLostActive;
	}

	/**
	 * @return Returns the itemConsume count over time.
	 */
	public final int getItemConsumeOT()
	{
		return _itemConsumeOT;
	}

	/**
	 * @return Returns the itemConsumeId over time.
	 */
	public final int getItemConsumeIdOT()
	{
		return _itemConsumeIdOT;
	}

	/**
	 * @return Returns the itemConsume count over time.
	 */
	public final int getItemConsumeSteps()
	{
		return _itemConsumeSteps;
	}

	/**
	 * @return Returns the itemConsume time in milliseconds.
	 */
	public final int getItemConsumeTime()
	{
		return _itemConsumeTime;
	}
}
